package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.pojo.Role;
import com.ccc.demoboot.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wz
 * @Date 2024/5/16 15:43
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: RoleController
 */

@RequestMapping("/role")
@RestController
public class RoleController {



    @Autowired
    RoleService roleService;

    /**
     * 分页查询所有角色
     * @param page
     * @return
     */
    @GetMapping("/select/{page}")
    public R select(@PathVariable("page") int page){
        PageHelper.startPage(page,5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Role> list = this.roleService.list();
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        return new R().add("rolePageInfo",rolePageInfo);

    }

    /**
     * 根据条件查询
     * @param role
     * @return
     */
    @PostMapping("/selectTj")
    public R selectTj(@RequestBody Role role){
//        PageHelper.startPage(page,5);//第一个参数表示第几页,第二个参数表示每页的条数
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.like("rolename",role.getRolename());
        List<Role> list = this.roleService.list(roleQueryWrapper);
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        return new R().add("rolePageInfo",rolePageInfo);
    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Role role){
        boolean save = this.roleService.save(role);
        System.out.println(role.getRemark());
        return new  R().add("save",save);
    }

    /**
     * 根据id删除
     * @param role
     * @return
     */
    @PostMapping("/del")
    public R del(@RequestBody Role role){
        boolean remove = this.roleService.removeById(role);
        return new  R().add("remove",remove);
    }

    /**
     * 根据id查询数据
     * @param role
     * @return
     */
    @PostMapping("/upselect")
    public R upselect(@RequestBody Role role){
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.setEntity(role);
        Role one = this.roleService.getOne(roleQueryWrapper);
        return new R().add("one",one);
    }

    /**
     * 修改数据
     * @param role
     * @return
     */
    @PostMapping("/up")
    public R up(@RequestBody Role role){
        boolean update = this.roleService.updateById(role);
        return new R().add("update",update);
    }

    /**
     * 下拉信息提取
     * @return
     */
    @PostMapping("/select")
    public R select(){
        List<Role> list = this.roleService.list();
        return new R().add("list",list);
    }




}
